﻿using Sorting.App.Models;


using Flurl.Http;
using Flurl;
using Sorting.App.Models.Enum;
using Modules.Units;

namespace Sorting.App.Services;

/// <summary>
/// 任务历史备份表
/// </summary>
public class TaskLogService
{



    ServiceApis serviceApis = new ServiceApis();



    /// <summary>
    /// 获取任务日志中的最大序号
    /// </summary>
    /// <returns></returns>
    public async Task<ResfulModel<int>> GetMaxTaskSeq()
    {
        try
        {

            var res = await serviceApis.GetMaxTaskSeqApi
                 .WithOAuthBearerToken( AppsettingUnits.Token)
                      .PostAsync()
                      .ReceiveJson<ResfulModel<int>>();


            return res;
        }
        catch (Exception ex)
        {

            throw;
        }


    }


    /// <summary>
    /// 新增储位信息
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public async Task<ResfulModel<bool>> AddTaskLog(TaskLogEntity entity)
    {
        return await serviceApis.AddTaskLogApi
             .WithOAuthBearerToken( AppsettingUnits.Token)
                  .PostJsonAsync(entity)
                   .ReceiveJson<ResfulModel<bool>>();

    }


    /// <summary>
    /// 更新储位信息
    /// </summary>
    /// <param name="inputentity"></param>
    /// <returns></returns>
    public async Task<ResfulModel<bool>> UpdateTaskLog(TaskLogEntity inputentity)
    {


        return await serviceApis.UpdateTaskLogApi
             .WithOAuthBearerToken( AppsettingUnits.Token)
                  .PostJsonAsync(inputentity)
                   .ReceiveJson<ResfulModel<bool>>();



    }

    /// <summary>
    /// 获取当前执行热任务根据任务序号
    /// </summary>
    /// <param name="Moudle"></param>
    /// <returns></returns>
    public async Task<TaskLogEntity> GetNowRunTaskBySeq()
    {

        var res= await serviceApis.GetNowRunTaskBySeqApi
             .WithOAuthBearerToken( AppsettingUnits.Token).PostAsync()
                     .ReceiveJson<ResfulModel<TaskLogEntity>>();


        return res.data;

    }


    /// <summary>
    /// 获取全部日志信息
    /// </summary>
    /// <returns></returns>
    public async Task<List<TaskLogEntity>> GetTaskLogEntities()
    {

        var res= await serviceApis.GetTaskLogEntitiesApi
             .WithOAuthBearerToken( AppsettingUnits.Token)
                   .PostAsync()
                     .ReceiveJson<ResfulModel<List<TaskLogEntity>>>();

        return res.data;
    }
}